package com.admin.scroller.photoorvideo;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.admin.scroller.photoorvideo.utils.TimeUtil;
import com.bumptech.glide.Glide;

import java.io.File;

/**
 * 视频预览界面
 */
public class VideoPreviewActivity extends AppCompatActivity implements View.OnClickListener {

    private VideoView videoView;
    private ImageView cancel;
    private ImageView ivPPlay;
    private SeekBar seekBar;
    private TextView textView;
    private ImageView preImage;
    private LinearLayout linearLayout;

    private String path;
    private int progress;

    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0x24){
                progress=videoView.getCurrentPosition();
                seekBar.setProgress(progress);
                setTextView();
                if(linearLayout.getVisibility()==View.VISIBLE){
                    linearLayout.setVisibility(View.GONE);
                }
                if(videoView.isPlaying()){
                    handler.sendEmptyMessageDelayed(0x24,1000);
                }else {
                    handler.removeMessages(0x24);
                }
            }else if(msg.what==0x25){
//                finish();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_preview);

        videoView=findViewById(R.id.vv_preview);
        cancel=findViewById(R.id.iv_back);
        ivPPlay=findViewById(R.id.iv_play_c);
        seekBar=findViewById(R.id.sb_video_progress);
        textView=findViewById(R.id.tv_play_progress);
        preImage=findViewById(R.id.iv_preview_image);
        linearLayout=findViewById(R.id.linear_pre);

        cancel.setOnClickListener(this);
        ivPPlay.setOnClickListener(this);

        path=getIntent().getStringExtra("url");

        Glide.with(this).load(new File(path)).into(preImage);

        videoView.setVideoPath(path);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                seekBar.setMax(mp.getDuration());
                videoView.start();
                ivPPlay.setImageResource(R.drawable.play);
                if(!handler.hasMessages(0x24)) {
                    handler.sendEmptyMessageDelayed(0x24,1000);
                }
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView.pause();
                ivPPlay.setImageResource(R.drawable.pause);
                handler.sendEmptyMessageDelayed(0x25,100);
            }
        });


    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.iv_back){
            if(videoView.isPlaying()){
                videoView.pause();
            }
            finish();
        }else if(v.getId()==R.id.iv_play_c){
            if(!TextUtils.isEmpty(path)) {
                if (videoView.isPlaying()) {
                    videoView.pause();
                    ivPPlay.setImageResource(R.drawable.pause);
                } else {
                    videoView.start();
                    ivPPlay.setImageResource(R.drawable.play);
                }
                if(!handler.hasMessages(0x24)) {
                    handler.sendEmptyMessage(0x24);
                }
            }
        }
    }

    private void setTextView(){
        textView.setText(TimeUtil.formatime(progress));
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(videoView.isPlaying()){
            videoView.pause();
        }
    }
}
